package net.lazyandroid.namewars.activity;

import net.lazyandroid.namewars.R;
import net.lazyandroid.namewars.model.LocalAccessor;
import net.lazyandroid.namewars.ui.MyDialogPreference;
import net.lazyandroid.namewars.utils.Constants;
import net.lazyandroid.namewars.utils.Tracker;
import android.content.DialogInterface;
import android.content.SharedPreferences;
import android.content.SharedPreferences.OnSharedPreferenceChangeListener;
import android.os.Bundle;
import android.preference.PreferenceActivity;

import com.flurry.android.FlurryAgent;

/**
 * @author Shane
 *
 */
public class SettingsActivity extends PreferenceActivity 
								implements MyDialogPreference.Listener,
								OnSharedPreferenceChangeListener 
{
	public static final String KEY_CHECKBOX_PREFERENCE = "checkbox_preference";
	public static final String KEY_CLEAR_FILE_CACHE_DIALOG  = "clear_file_cache_dialog";
	//private CheckBoxPreference mAutoSaveFightingRecords ;
	private MyDialogPreference mClearFileCacheDialog;
	
	
	@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        
        // Load the preferences from an XML resource
        addPreferencesFromResource(R.xml.preferences);
        setupViews();
        Tracker.start(this);
    }
	
	private void setupViews()
	{
		/**
		mAutoSaveFightingRecords = (CheckBoxPreference)getPreferenceScreen().
									findPreference(KEY_CHECKBOX_PREFERENCE) ;
		*/
		mClearFileCacheDialog = (MyDialogPreference)getPreferenceScreen().
								findPreference( KEY_CLEAR_FILE_CACHE_DIALOG);
		mClearFileCacheDialog.setListener(this);
		update();
	}
	
	private boolean isUpdating;

    private synchronized void update() {
        if (isUpdating) {
            return;
        }
        isUpdating = true;
        try {
            update0();
        } finally {
            isUpdating = false;
        }
    }
	
    private void update0() 
    {
    	mClearFileCacheDialog.setDialogLayoutResource(R.layout.clear_file_cache_dialog);
    	mClearFileCacheDialog.setDialogTitle(R.string.clear_cache_dialog_title);
    	mClearFileCacheDialog.setDialogMessage(R.string.clear_cache_dialog_content);
    }
    
    public void onClick(MyDialogPreference dialog, int which) 
    {
        if (dialog == mClearFileCacheDialog) 
        {
            if (which == DialogInterface.BUTTON1) 
            {
            	LocalAccessor.getInstance(SettingsActivity.this).setAvailableID(0);
                LocalAccessor.getInstance(SettingsActivity.this).clearFightRecords();
            }
        }
    }
	@Override
    protected void onStart() 
    {
    	super.onStart();
    	FlurryAgent.onStartSession(this,Constants.FLURRY_API_KEY);
    }
    
    /*
     * @see android.app.Activity#onStop()
     */
    @Override
    protected void onStop() 
    {
    	super.onStop();
    	FlurryAgent.onEndSession(this) ; 
    }
	
	@Override
	protected void onRestart() 
	{
		super.onRestart();
	}
	@Override
	protected void onDestroy() 
	{
		super.onDestroy();
		Tracker.stop();
	}
	
	 @Override
	 protected void onResume() 
	 {
        super.onResume();
        // Set up a listener whenever a key changes
        getPreferenceScreen().getSharedPreferences().registerOnSharedPreferenceChangeListener(this);
	 }

    @Override
    protected void onPause() 
    {
        super.onPause();
        // Unregister the listener whenever a key changes
        getPreferenceScreen().getSharedPreferences().unregisterOnSharedPreferenceChangeListener(this);
    }

    public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) 
    {
        // Let's do something when my counter preference value changes
        if (key.equals(KEY_CHECKBOX_PREFERENCE)) 
        {
        	//Log.d("mAutoSaveFightingRecords: "+mAutoSaveFightingRecords.isChecked()) ;
        }
    }
	    
}
